%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               ReplicateTableA4.m
%
% Author:             Tasos Kalandrakis
%
% Description:        Script that replicates Table A4
%
% Created:            Sep - 2021
%
% Last Modified:      Sep - 2021
%
% Language:           MATLAB
%
% Related References: [1] Kalandrakis, Tasos. 2022. "One-dimensional 
%                     scaling without apologies," Journal of Politics, 
%                     forthcoming.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
filedir=pwd;

xdir=strcat(filedir,'/XTests/');
addpath(xdir)
xdir=strcat(filedir,'/Inference/');
addpath(xdir)
xdir=strcat(filedir,'/TwoStepMatlab/');
addpath(xdir)
Surveys=strcat(filedir,'/CIRC/GERN.mat');
load(Surveys)

TestMani
TestChapel

rdir=strcat(filedir,'/TESTRESULTS/');

load(strcat(rdir,'ManiTests3.mat'))
TMani3=[ManiTests.rej98 ManiTests.rej02 ManiTests.rej05 ManiTests.rej09 ManiTests.rej13 ManiTests.rej17;
    ManiTests.s98 ManiTests.s02 ManiTests.s05 ManiTests.s09 ManiTests.s13 ManiTests.s17;
ManiTests.crVa98 ManiTests.crVa02 ManiTests.crVa05 ManiTests.crVa09 ManiTests.crVa13 ManiTests.crVa17];
load(strcat(rdir,'ManiTests2.mat'))
TMani2=[ManiTests.rej98 ManiTests.rej02 ManiTests.rej05 ManiTests.rej09 ManiTests.rej13 ManiTests.rej17;
    ManiTests.s98 ManiTests.s02 ManiTests.s05 ManiTests.s09 ManiTests.s13 ManiTests.s17;
ManiTests.crVa98 ManiTests.crVa02 ManiTests.crVa05 ManiTests.crVa09 ManiTests.crVa13 ManiTests.crVa17];
load(strcat(rdir,'ManiTests1.mat'))
TMani1=[ManiTests.rej98 ManiTests.rej02 ManiTests.rej05 ManiTests.rej09 ManiTests.rej13 ManiTests.rej17;
    ManiTests.s98 ManiTests.s02 ManiTests.s05 ManiTests.s09 ManiTests.s13 ManiTests.s17;
ManiTests.crVa98 ManiTests.crVa02 ManiTests.crVa05 ManiTests.crVa09 ManiTests.crVa13 ManiTests.crVa17];

TMani=[TMani3;TMani2;TMani1;[ManiTests.P98 ManiTests.P02 ManiTests.P05 ManiTests.P09 ManiTests.P13 ManiTests.P17];[ManiTests.N98 ManiTests.N02 ManiTests.N05 ManiTests.N09 ManiTests.N13 ManiTests.N17]];

load(strcat(rdir,'ChapelTests3.mat'))
TChapel3=[ChapelTests.rej98 ChapelTests.rej02 ChapelTests.rej05 ChapelTests.rej09 ChapelTests.rej13 ChapelTests.rej17;
    ChapelTests.s98 ChapelTests.s02 ChapelTests.s05 ChapelTests.s09 ChapelTests.s13 ChapelTests.s17;
ChapelTests.crVa98 ChapelTests.crVa02 ChapelTests.crVa05 ChapelTests.crVa09 ChapelTests.crVa13 ChapelTests.crVa17];
load(strcat(rdir,'ChapelTests2.mat'))
TChapel2=[ChapelTests.rej98 ChapelTests.rej02 ChapelTests.rej05 ChapelTests.rej09 ChapelTests.rej13 ChapelTests.rej17;
    ChapelTests.s98 ChapelTests.s02 ChapelTests.s05 ChapelTests.s09 ChapelTests.s13 ChapelTests.s17;
ChapelTests.crVa98 ChapelTests.crVa02 ChapelTests.crVa05 ChapelTests.crVa09 ChapelTests.crVa13 ChapelTests.crVa17];
load(strcat(rdir,'ChapelTests1.mat'))
TChapel1=[ChapelTests.rej98 ChapelTests.rej02 ChapelTests.rej05 ChapelTests.rej09 ChapelTests.rej13 ChapelTests.rej17;
    ChapelTests.s98 ChapelTests.s02 ChapelTests.s05 ChapelTests.s09 ChapelTests.s13 ChapelTests.s17;
ChapelTests.crVa98 ChapelTests.crVa02 ChapelTests.crVa05 ChapelTests.crVa09 ChapelTests.crVa13 ChapelTests.crVa17];

TChapel=[TChapel3;TChapel2;TChapel1;[ChapelTests.P98 ChapelTests.P02 ChapelTests.P05 ChapelTests.P09 ChapelTests.P13 ChapelTests.P17];[ChapelTests.N98 ChapelTests.N02 ChapelTests.N05 ChapelTests.N09 ChapelTests.N13 ChapelTests.N17]];

TableA4c=[1998 2002 2005 2009 2013 2017;TMani([2 3 5 6 8 9 10 11],:); TChapel([2 3 5 6 8 9 10 11],:)];


TableA4=cell(17,8);
TableA4{1,2}='Year';
TableA4{2,2}='MAX';
TableA4{3,2}='Critical level';
TableA4{5,2}='Critical level';
TableA4{7,2}='Critical level';
TableA4{11,2}='Critical level';
TableA4{13,2}='Critical level';
TableA4{15,2}='Critical level';
TableA4{4,2}='QLR';
TableA4{6,2}='MMM';
TableA4{8,2}='J';
TableA4{9,2}='N';
TableA4{10,2}='MAX';
TableA4{12,2}='QLR';
TableA4{14,2}='MMM';
TableA4{16,2}='J';
TableA4{17,2}='N';
TableA4{2,1}='Manifesto';
TableA4{10,1}='Chapel Hill';

for j=3:8
    for i=1:17
        TableA4{i,j}=TableA4c(i,j-2);
    end
end
openvar('TableA4')